using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

namespace Life
{
    /// <summary>
    /// Text with a border
    /// </summary>
    class TextBox : TextControl
    {
        private IAsyncResult kbResult;
        private string _title;
        private string _description;

        // Border
        public int BorderThickness = 4;
        public Color BorderColor = new Color(200, 200, 200);
        public Color FillColor = new Color(100, 100, 100) * .75f;
        public Color TextColor = Color.White;
        public float Alpha = 1f;

        public TextBox(string title, string description, string text, SpriteFont font, Color color, Vector2 position, Vector2 size)
            : base(text, font, color, position, size)
        {
            _title = title;
            _description = description;
            Tapped += OnTapped;
        }

        private void OnTapped(object sender, EventArgs args)
        {
            // TODO: Why can this be called twice in the wild?
            // TODO: Unable to repro locally

            try
            {
                if (Guide.IsVisible == false)
                {
                    kbResult = Guide.BeginShowKeyboardInput(PlayerIndex.One,
                            _title, _description,
                            ((Text == null) ? "" : Text),
                            GetTypedChars, null);
                }
            }
            catch (Exception)
            {
                // TODO: handle exception
            }
        }

        private void GetTypedChars(IAsyncResult r)
        {
            string text = Guide.EndShowKeyboardInput(r);

            if (text != null)
                Text = text;
        }

        public override void Draw(DrawContext context)
        {
            DrawBorder(context);

            base.Draw(context);
        }

        private void DrawBorder(DrawContext context)
        {
            SpriteBatch spriteBatch = context.SpriteBatch;
            SpriteFont font = context.ButtonFont;
            Texture2D blank = context.BlankTexture;

            Vector2 absPos = Position + context.DrawOffset;

            // Compute the button's rectangle
            Rectangle r = new Rectangle(
                (int)absPos.X,
                (int)absPos.Y,
                (int)Size.X,
                (int)Size.Y);

            // Fill the button
            spriteBatch.Draw(blank, r, FillColor * Alpha);

            // Draw the border
            spriteBatch.Draw(
                blank,
                new Rectangle(r.Left, r.Top, r.Width, BorderThickness),
                BorderColor * Alpha);
            spriteBatch.Draw(
                blank,
                new Rectangle(r.Left, r.Top, BorderThickness, r.Height),
                BorderColor * Alpha);
            spriteBatch.Draw(
                blank,
                new Rectangle(r.Right - BorderThickness, r.Top, BorderThickness, r.Height),
                BorderColor * Alpha);
            spriteBatch.Draw(
                blank,
                new Rectangle(r.Left, r.Bottom - BorderThickness, r.Width, BorderThickness),
                BorderColor * Alpha);
        }
    }
}
